<template>
  <div class="dashboard-container">
    <div class="refundrAll">
      <el-badge :value="totalArr[0]" :max="99999" :hidden="totalArr[0] === 0 ? true : false" class="btn-item">
        <el-button :class="{on:type === 0}" @click="returnGoods(0)">全部申请</el-button>
      </el-badge>
      <el-badge :value="totalArr[1]" :max="99999" :hidden="totalArr[1] === 0 ? true : false" class="btn-item">
        <el-button :class="{on:type === 1}" @click="returnGoods(1)">待处理</el-button>
      </el-badge>
      <el-badge :value="totalArr[2]" :max="99999" :hidden="totalArr[2] === 0 ? true : false" class="btn-item">
        <el-button :class="{on:type === 2}" @click="returnGoods(2)">退款成功</el-button>
      </el-badge>
      <el-badge :value="totalArr[3]" :max="99999" :hidden="totalArr[3] === 0 ? true : false" class="btn-item">
        <el-button :class="{on:type === 3}" @click="returnGoods(3)">退款失败</el-button>
      </el-badge>
    </div>
    <el-row :gutter="20">
      <el-col :span="19" class="s18">
        <div class="grid-content1 bg-purple-dark" @click="show3 = !show3">
          <span><i :class="show3? 'el-icon-caret-top' : 'el-icon-caret-bottom'"/> {{ show3? '收起筛选' : '展开筛选' }}</span>
        </div>
      </el-col>
      <el-col :span="5" class="s6">
        <div class="grid-content bg-purple-dark">
          <el-button size="small">查询结果</el-button>
          <!-- <el-button size="small">高级检索</el-button> -->
        </div>
      </el-col>
    </el-row>
    <el-collapse-transition>
      <div v-show="show3">
        <el-row :gutter="20" class="queryConditions">
          <!-- 报表月份 -->
          <el-col :span="8">
            <el-row>
              <el-col :span="6"><span>报表月份</span></el-col>
              <el-col :span="15">
                <el-date-picker
                  v-model="dataTime"
                  :picker-options="pickerOptions"
                  :default-time="['00:00:00', '23:59:59']"
                  type="datetimerange"
                  range-separator="-"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"/>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="queryConditions" style="margin-top: 20px;">

          <el-col :span="24">
            <el-row>
              <el-col :span="2"><span>时间选择</span></el-col>
              <el-col :span="18">
                <div>
                  <el-date-picker
                    v-model="value1"
                    :picker-options="{
                      disabledDate(time) {
                        return time.getTime() > Date.now();
                      }
                    }"
                    type="date"
                    placeholder="开始日期"
                  />
                  <el-time-picker
                    v-model="value2"
                    :picker-options="{
                      selectableRange: pickerOptions2
                    }"
                    :disabled="value1 === ''"
                    placeholder="开始时间"
                    @focus="gitHMS"
                  />
                  >
                  <el-date-picker
                    v-model="value3"
                    :picker-options="{
                      disabledDate(time) {
                        return value1 > time.getTime() || time.getTime() > Date.now();
                      }
                    }"
                    :disabled="value2 === ''"
                    type="date"
                    placeholder="结束日期"
                  />
                  <el-time-picker
                    v-model="value4"
                    :picker-options="{
                      selectableRange: pickerOptions3
                    }"
                    :disabled="value3 === ''"
                    placeholder="结束时间"
                    @focus="gitHMS2"
                  />
                </div>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </div>
    </el-collapse-transition>

    <div class="title">
      <svg-icon icon-class="dataList" class-name="custom-class"/>
      <p style="display: inline;">数据列表</p>
    </div>

    <!-- 表格 -->
    <el-table
      v-loading="logding"
      ref="multipleTable"
      :row-key="getRowKeys"
      :data="tableData.slice((pageno - 1) * pagesize, pageno * pagesize)"
      style="width: 100%"
      border
      tooltip-effect="dark"
      @selection-change="handleSelectionChange">

      <el-table-column :reserve-selection="false" type="selection" width="50"/>

      <!-- 订单id -->
      <el-table-column label="订单id">
        <template slot-scope="scope" width="100px">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <!-- 编号 -->
      <el-table-column label="编号">
        <template slot-scope="scope" width="100px">
          <span>{{ scope.row.skuSn }}</span>
        </template>
      </el-table-column>

      <!-- 产品名称 -->
      <el-table-column label="产品名称">
        <template slot-scope="scope" width="100px">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <!-- 产品图片 -->
      <el-table-column label="产品图片">
        <template slot-scope="scope" width="100px">
          <span>
            <img :src="scope.row.img" style="width: 50px; height: 50px;">
          </span>
        </template>
      </el-table-column>

      <el-table-column label="商品规格" width="180">
        <template slot-scope="scope">
          <p v-if="scope.row.value === null">无</p>
          <div v-else>
            <el-popover trigger="hover" placement="top">
              <el-row v-for="(item,index) in JSON.parse(scope.row.value)" :key="index">
                <el-col>{{ item.key }}：{{ item.value }}</el-col>
              </el-row>
              <div slot="reference" class="name-wrapper">
                <el-tag size="medium">{{ scope.row.value | skuFilterList }}</el-tag>
              </div>
            </el-popover>
          </div>
        </template>
      </el-table-column>

      <!-- 价格 -->
      <el-table-column label="价格">
        <template slot-scope="scope" width="100px">
          <span>{{ scope.row.pic * 100 / 10000 }}</span>
        </template>
      </el-table-column>

      <!-- 产品备注 -->
      <el-table-column label="产品备注">
        <template slot-scope="scope" width="100px">
          <span>{{ scope.row.csName }}</span>
        </template>
      </el-table-column>

      <!-- 订单类型 -->
      <el-table-column label="订单类型">
        <template slot-scope="scope" width="100px">
          <span>{{ scope.row.csType | staFilter }}</span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码 -->
    <div class="pagination">
      <el-pagination
        :page-size="pagesize"
        :total="tableData.length"
        layout="total, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>
  </div>
</template>

<script>
import { returnGoods } from '@/api/order'
export default {
  name: 'Return',
  filters: {
    staFilter(status) {
      const staMap = {
        1: '待处理',
        2: '退款成功',
        3: '退款失败'
      }
      return staMap[status]
    },
    // 规格
    skuFilterList(value) {
      let item = ''
      const val = JSON.parse(value)
      for (let i = 0; i < val.length; i++) {
        item = val[i].value + ',' + item
      }
      return item.substring(0, item.length - 1)
    }
  },
  data() {
    return {
      totalArr: [34, 8, 14, 12], // 存放提示数字
      type: 0, // 用于el-button样式
      show3: true, // 显示筛选
      dataTime: '', // 提交时间
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      logding: false, // 加载
      getRowKeys(row) { // 翻页全选
        return row.orderId
      },
      tableData: [], // 表格数据
      pagesize: 10, // 每页显示20条
      pageno: 1, // 显示第一页
      input1: '',
      value1: '',
      value2: '',
      value3: '',
      value4: '',
      pickerOptions2: '',
      pickerOptions3: '',
      clock1: ''
    }
  },
  created() {
    this.returnGoods()
    this.gitHMS()
  },
  methods: {
    gitHMS() {
      var now1 = new Date()
      var hh1 = now1.getHours()
      var mm1 = now1.getMinutes()
      var ss1 = now1.getSeconds()
      var clock = ''
      if (hh1 < 10) clock += '0'
      clock += hh1 + ':'
      if (mm1 < 10) clock += '0'
      clock += mm1 + ':'
      if (ss1 < 10) clock += '0'
      clock += ss1
      this.clock1 = clock
      this.pickerOptions2 = '00:00:00' + '-' + clock
      console.log(this.pickerOptions2)
    },
    gitHMS2() {
      var now2 = new Date()
      var hh2 = now2.getHours()
      var mm2 = now2.getMinutes()
      var ss2 = now2.getSeconds()
      var clock2 = ''
      if (hh2 < 10) clock2 += '0'
      clock2 += hh2 + ':'
      if (mm2 < 10) clock2 += '0'
      clock2 += mm2 + ':'
      if (ss2 < 10) clock2 += '0'
      clock2 += ss2
      this.pickerOptions3 = this.clock1 + '-' + clock2
      console.log(this.clock1)
    },
    // 多选
    handleSelectionChange(rows) {
      this.multipleSelection = rows
    },
    handleSizeChange(size) {
      this.pagesize = size
    },
    handleCurrentChange(currentPage) {
      this.pageno = currentPage
    },
    returnGoods(val) {
      // console.log(val)
      const self = this
      self.type = val || 0
      self.logding = true
      const params = new URLSearchParams()
      returnGoods(params).then(res => {
        // console.log(res)
        if (res.code === 200) {
          self.logding = false
          if (val === undefined || val === 0) {
            self.tableData = res.result
          } else {
            self.pageno = 1
            const arr = res.result.filter(item => {
              return item.csType === val
            })
            self.tableData = arr
          }
        } else {
          self.logding = false
          self.tableData = []
        }
      })
    }
  }
}
</script>

<style scoped>
.dashboard-container {
  background-color: #fff;
  padding: 30px 20px 20px 20px;
}
.grid-content {
  min-height: 50px;
  line-height: 50px;
  margin-bottom: 20px;
  cursor: pointer;
}
.grid-content1 {
  min-height: 50px;
  line-height: 50px;
  margin-bottom: 20px;
  cursor: pointer;
  text-align: right;
  padding-right: 20px;
}
.bg-purple-dark {
  background: rgba(243, 243, 243, 1);
}
.s18 {
  padding-right: 0!important;
}
.s6 {
  padding-left: 0!important;
}
.queryConditions span {
  font-size: 16px;
  line-height: 40px;
}
.title {
  text-align: left;
  height: 50px;
  line-height: 50px;
  padding-right: 20px;
  padding-left: 20px;
  margin-top: 40px;
  background: rgba(243, 243, 243, 1);
}
.dashboard-container >>> .el-badge__content.is-fixed {
  top: 2px!important;
}
.pagination {
  margin: 30px auto;
  text-align: center;
}
.refundrAll{
  margin-bottom: 20px;
}
.btn-item{
  margin-right: 40px;
}
.btn-item .on{
  color: #409EFF !important;
  background: #ecf5ff;
  border-color: #b3d8ff
}
</style>
